﻿using AutoMapper;
using dreamwork.mobile.app.api.Models.Data;
using dreamwork.mobile.app.api.Models.ViewModel;
using dreamwork.mobile.app.api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace dreamwork.mobile.app.api.Controllers
{
    public class TerminalUserController : ControllerBase
    {

        private readonly ITerminalUserRegisterService _terminalUserRegisterService;
        private readonly ITerminalUserService _terminalUserService;
        private readonly IMapper _mapper;
        public TerminalUserController(
            ITerminalUserRegisterService terminalUserRegisterService,
            ITerminalUserService terminalUserService,
            IMapper mapper)
        {
            _terminalUserRegisterService = terminalUserRegisterService;
            _terminalUserService = terminalUserService;
            _mapper = mapper;
        }

        [HttpGet]
        [Route("[action]")]
        public async Task<IActionResult> GetRegistrationCodeAsync(string phoneNumber)
        {
            return Success(await _terminalUserRegisterService.GetRegistrationCodeAsync(phoneNumber));
        }
        [HttpPost]
        [Route("[action]")]
        public async Task<IActionResult> RegisterAsync([FromBody]TerminalUserRegisterViewModel user)
        {
            var result = await _terminalUserRegisterService.RegisterAsync(_mapper.Map<TerminalUser>(user));
            if (result.Item1)
                return Success(result.Item2);
            return Error(result.Item2);

        }


        [HttpPost]
        [Route("[action]")]
        public async Task<IActionResult> LoginAsync([FromBody]TerminalUserLoginViewModel normalUser)
        {
            return Success(await _terminalUserService.LoginAsync(_mapper.Map<TerminalUser>(normalUser)));
        }
        [HttpGet]
        [Route("[action]")]
        public async Task<IActionResult> LogoutAsync()
        {
            return Success(await _terminalUserService.LogoutAsync());
        }
        [HttpGet]
        [Route("[action]")]
        [Authorize]
        public async Task<IActionResult> GetUserProfileAsync()
        {
            if (string.IsNullOrEmpty( CurrentUserId))
            {
                return Error("请登录.");
            }
            var result = await _terminalUserService.GetById(CurrentUserId);
            if (result.IsSuccess)
            {
                return Success(_mapper.Map<TerminalUserDetailViewModel>(result.Data));
            }
            return Error(result.Message);

        }

        [HttpPost]
        [Route("[action]")]
        public async Task<IActionResult> ModifyAsync([FromBody]TerminalUserModifyViewModel user)
        {
            var result = await _terminalUserService.ModifyAsync(_mapper.Map<TerminalUser>(user));
            if (result.IsSuccess)
            {
                return Success(_mapper.Map<TerminalUserDetailViewModel>(result.Data));
            }
            return Error(result.Message);
        }

        [HttpPost]
        [Route("[action]")]
        public async Task<IActionResult> ModifyPositionAsync([FromBody]TerminalUserUpdatePositionViewModel normalUser)
        {
            var result = await _terminalUserService.UpdatePositionAsync(_mapper.Map<TerminalUser>(normalUser));
            if (result.IsSuccess)
            {
                return Success(_mapper.Map<TerminalUserDetailViewModel>(result.Data));
            }
            return Error(result.Message);
        }

        [HttpGet]
        [Route("[action]")]
        public async Task<IActionResult> GetTerminalListAsync()
        {
            var result = await _terminalUserService.GetListAsync();
            if (result.IsSuccess)
            {
                return Success(_mapper.Map<IEnumerable<TerminalUserDetailViewModel>>(result.Data));
            }
            return Error(result.Message);
        }

    }
}
